<?php namespace Phpcmf\Controllers\Admin;

class Home extends \Phpcmf\Admin\Module
{

	public function index() {
		$this->_Admin_List();
	}

	public function add() {
		$this->_Admin_Add();
	}
	/*
	$son_type = '{\"1\":{\"id\":1,\"name\":\"综合\",\"fuwuxian\":\"1\",\"yanse\":\"\",\"paixu\":\"\"}}';
	*/

	public function edit() {
		$this->_Admin_Edit();
	}

	public function show_index() {
		$this->_Admin_Show();
	}

	public function order_edit() {
		$this->_Admin_Order();
	}

	public function move_edit() {
		$this->_Admin_Move();
	}

	public function tui_edit() {
		$this->_Admin_Send();
	}

	public function syncat_edit() {
		$this->_Admin_Syncat();
	}

	public function del() {
		//$this->_Admin_Del();
		if (IS_POST) {
		
		    $ids = \Phpcmf\Service::L('input')->get_post_ids();
		    if (!$ids) {
		        $this->_json(0, dr_lang('没有选择内容'));
		    }
		
		    $rt = $this->content_model->delete_to_recycle($ids, \Phpcmf\Service::L('input')->post('note'));
		    if ($rt['code']) {
		        // 写入日志
		        \Phpcmf\Service::M('cache')->update_data_cache();
		        \Phpcmf\Service::L('input')->system_log($this->name.'：放入回收站id ('.implode(', ', $ids).')');

				foreach ($ids as $t) {
					// 删除评论
					$tieid = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('cid', $ids)->get()->getRowArray();
					$tieid = $tieid['id'];
					\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_comment')->where('cid', $tieid)->delete();
					\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_comment_index')->where('cid', $tieid)->delete();
					// 删除帖子收藏
					\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_favorite')->where('cid', $tieid)->delete();
				    // 删除加入
				    \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_cart')->where('cid', $t)->delete();
					// 删除帖子
					\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('cid', $t)->delete();
					// 删除帖子统计
					\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_hits')->where('id', $tieid)->delete();
					// 删除帖子点赞
					\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_zan')->where('cid', $tieid)->delete();
				}
		        $this->_json(1, dr_lang('所选内容已被放入回收站中'));
		    } else {
		        $this->_json(0, $rt['msg']);
		    }
			
		} else {
		    // 选择选项
			
		    $ids = $_GET['ids'];
		    if (!$ids) {
		        $this->_json(0, dr_lang('没有选择内容'));
		    }
			
		    \Phpcmf\Service::V()->assign([
		        'ids' => $ids,
		        'delete_msg' => $this->module['setting']['delete_msg'] ? explode(PHP_EOL, (string)$this->module['setting']['delete_msg']) : [],
		    ]);
			
		    \Phpcmf\Service::V()->display('share_delete.html');
		    exit;
		}
		/*$jiarus = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_cart')->where('cid', $ids)->get()->getRowArray();
		dr_debug('t', $jiarus);*/
		
		// 执行删除
		/*$this->_Del(\Phpcmf\Service::L('input')->get_post_ids(), null, function($rows) {
		    foreach ($rows as $t) {
				
				dr_debug('$t', $t);
		        // 删除索引
		        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_cart')->delete($t['id']);
		        // 删除审核提醒
		        \Phpcmf\Service::M('member')->delete_admin_notice($this->module['dirname'].'/verify/edit:id/'.$t['id'], SITE_ID);
		    }
		    return dr_return_data(1, 'ok');
		});*/
		
	}
	//dr_module_table_prefix(APP_DIR).'_recycle'
	// 格式化保存数据
	protected function _Format_Data($id, $data, $old) {
	
	    // 验证栏目
	    $catid = (int)\Phpcmf\Service::L('input')->post('catid');
	    $cat = dr_cat_value($this->module['mid'], $catid);
	    if (!$cat && !$this->is_hcategory) {
	        $this->_json(0, dr_lang('栏目[%s]不存在', $catid));
	    }
	
	    // 验证后台权限
	    $data[1]['catid'] = $data[0]['catid'] = $catid;
	
	    // 验证状态
	    if ($this->is_post_user) {
	        // 投稿者
	        $data[1]['status'] = \Phpcmf\Service::M('auth')->is_post_user_status() ? 1 : 9;
	    } else {
	        $data[1]['status'] = 9;
	    }
	
	    $data[1]['uid'] = (int)$data[1]['uid'];
	
	    // 默认数据
	    $data = $this->content_model->format_data($data);
		if (!$old[1]['son_type']) {
			$data[1]['son_type'] = '{"1":{"id":1,"name":"综合","fuwuxian":"1","yanse":"","paixu":""}}';
		}
	
	    // 不更新时间
	    if (!$old['updatetime']) {
	        !$data[1]['updatetime'] && $data[1]['updatetime'] = SYS_TIME;
	    } elseif ($id && isset($_POST['no_time'])
	        && $_POST['no_time']) {
	        $data[1]['updatetime'] = $old['updatetime'];
	    }
	
	    return $data;
	}
	/**
	 * 回调处理结果
	 * $data
	 * */
	protected function _Call_Post($data) {
		// 添加圈主
		$tianjia = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_cart')
			->where('cid', $data[1]['id'])
			->where('uid', $data[1]['uid'])
			->get()->getRowArray();
		if (!$tianjia) {
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_cart')->insert([
				'cid' => $data[1]['id'],
				'uid' => $data[1]['uid'],
				'read_name' => '',
				'read_url' => '',
				'inputtime' => SYS_TIME,
			]);
		}
		//更新人数统计数量
		$jiarutongji = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_cart')->where('cid', $data[1]['id'])->countAllResults();
		// 更新记录表->where('id', $row['id'])->update(['status' => 2])
		\Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR))->where('id', $data[1]['id'])->update([
			'joins' => $jiarutongji,
		]);
		
	
	    if ($data[1]['status'] == 9) {
	        $html = $list = '';
	        $cat = dr_cat_value($this->module['mid'], $data[1]['catid']);
	        if ($cat['setting']['html']) {
	            // 生成权限文件
	            if (!dr_html_auth(1)) {
	                $this->_json(0, dr_lang('/cache/html/ 无法写入文件'));
	            }
	            $html = dr_web_prefix('index.php?s='.MOD_DIR.'&c=html&m=showfile&id='.$data[1]['id']);
	            $list = dr_web_prefix('index.php?s='.MOD_DIR.'&c=html&m=categoryfile&id='.$data[1]['catid']);
	        }
	        $this->_json(1, dr_lang('操作成功'), [
	            'id' => $data[1]['id'],
	            'url' => isset($_GET['is_self']) ? dr_url(MOD_DIR.'/home/edit', ['id' => $data[1]['id']]) : '',
	            'catid' => $data[1]['catid'],
	            'htmlfile' => $html,
	            'htmllist' => $list
	        ]);
	    } else {
	        if (intval(\Phpcmf\Service::L('input')->post('is_draft'))) {
	            // 草稿
	            $this->_json(1, dr_lang('保存草稿成功'));
	        } elseif ($this->is_post_user) {
	            // 投稿者
	            $this->_json(1, dr_lang('操作成功，等待管理员审核'), [
	                'url' => dr_url($this->_is_admin_auth(MOD_DIR.'/verify/index') ? MOD_DIR.'/verify/index' : MOD_DIR.'/home/index'),
	                'id' => $data[1]['id'],
	                'catid' => $data[1]['catid'],
	            ]);
	        }
	        $this->_json(1, dr_lang('操作成功'), [
	            'id' => $data[1]['id'],
	            'catid' => $data[1]['catid']
	        ]);
	    }
	}
}
